package com.neusoft.controller;

import com.neusoft.po.Comment;
import com.neusoft.po.CommonResult;
import com.neusoft.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/CommentController")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/listCommentByBusinessId/{businessId}")
    public CommonResult<List> listCommentByBusinessId(@PathVariable("businessId") Integer businessId) throws Exception{
        List<Comment> result = commentService.listCommentByBusinessId(businessId);
        return new CommonResult(200, "success", result);
    }

    @PostMapping("/saveComment/{id}/{userId}/{businessId}/{content}")
    public CommonResult<Integer> saveComment(@PathVariable("id") Integer id,
                                             @PathVariable("userId") String userId,
                                             @PathVariable("businessId") Integer businessId,
                                             @PathVariable("content") String content) throws Exception {
        Comment comment = new Comment();
        comment.setId(id);
        comment.setUserId(userId);
        comment.setBusinessId(businessId);
        comment.setContent(content);
        int result = commentService.saveComment(comment);
        return new CommonResult(200, "success", result);
    }

    @PostMapping("/updateComment/{id}/{content}")
    public CommonResult<Integer> updateComment(@PathVariable("id") Integer id,
                                               @PathVariable("content") String content) throws Exception {
        int result = commentService.updateComment(id, content);
        return new CommonResult(200, "success", result);
    }

    @DeleteMapping("/removeComment/{id}")
    public CommonResult<Integer> removeComment(@PathVariable("id") Integer id) throws Exception {
        int result = commentService.removeComment(id);
        return new CommonResult(200, "success", result);
    }
}
